package com.zkca.xf.ui.activity.personprotect;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.FaultJobLBean;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.presenter.FaultJobLPresenter;
import com.zkca.xf.ui.activity.nfc.Activity_saomiao;
import com.zkca.xf.ui.adapter.SingleAdapter;
import com.zkca.xf.ui.adapter.ViewHolder;
import com.zkca.xf.utils.DisplayUtil;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.SpaceItemDecoration;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.FaultJobLView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FaultJob_LActivity extends BaseTitleActivity implements FaultJobLView {

    @BindView(R.id.xrv_task)
    XRecyclerView xrvTask;
    @BindView(R.id.wushuju)
    ImageView wushuju;
    @BindView(R.id.xre_dtype)
    XRecyclerView xreDtype;
    @BindView(R.id.shaixuan_begantime)
    TextView shaixuanBegantime;
    @BindView(R.id.shaixuanbeganImage)
    ImageView shaixuanbeganImage;
    @BindView(R.id.bgtime)
    RelativeLayout bgtime;
    @BindView(R.id.shaixuan_overtime)
    TextView shaixuanOvertime;
    @BindView(R.id.shaixuan_overimage)
    ImageView shaixuanOverimage;
    @BindView(R.id.ovtime)
    RelativeLayout ovtime;
    @BindView(R.id.shaixuan_cancel)
    TextView shaixuanCancel;
    @BindView(R.id.shaixuan_sure)
    TextView shaixuanSure;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    private List<FaultJobLBean.DataBean> dataBeanList = new ArrayList<>();
    private SingleAdapter<FaultJobLBean.DataBean> adapter;

    private FaultJobLPresenter faultJobLPresenter;
    private RequestInspect requestInspect;
    int pageNum = 1;
    int pageSize = 10;
    private int jobResult=0;
    private Drawable dzx,ywc,gq,sx,wz;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_l);
        ButterKnife.bind(this);
        faultJobLPresenter = new FaultJobLPresenter(this, this);

        initMyView();
        initMyAdapter();

    }

    private void initMyAdapter() {
        adapter = new SingleAdapter<FaultJobLBean.DataBean>(mContext, dataBeanList, R.layout.faultjobitem) {
            @Override
            public void convert(ViewHolder holder, final FaultJobLBean.DataBean dataBean, int position) {
                String devImage4 = (String) dataBean.getDevImage4();
//                LogUtil.e(dataBean.getName3()+"图片：",devImage4+"");
                String name3 = dataBean.getName3();//故障编号
                final int handleResult = dataBean.getHandleResult();//执行状态  1 为执行 2 已执行
                String handleUname = dataBean.getHandleUname();//指派用户
                String devName = dataBean.getDevName();//设备名称
                String installPosition = dataBean.getInstallPosition();//设备地点
                String name1 = dataBean.getName1();//问题描述

                TextView jobnum = holder.getView(R.id.jobnum);
                TextView jobstatus_t = holder.getView(R.id.jobstatus);//状态
                TextView jobuser = holder.getView(R.id.jobuser);//维修人员
                TextView devicename = holder.getView(R.id.devicename);//设备名称
                TextView devicepasion = holder.getView(R.id.devicepasion);//设备地点
                TextView common = holder.getView(R.id.common);//问题描述
                boolean contains = name1.contains("<br/>");
                if (contains){
                   name1= name1.replace("<br/>","注：");
                }
//                name1=name1.Replace("<br/>","");
//                common.setText(name1);
                devicepasion.setText(installPosition);
                devicename.setText(devName);
                jobuser.setText(handleUname);
                jobnum.setText(name3);

                common.setText(name1);
                if (handleResult==1){
                    jobstatus_t.setText("待维修");
                    jobstatus_t.setTextColor(Color.rgb(213, 66, 68));
                    jobstatus_t.setCompoundDrawablesWithIntrinsicBounds(dzx,
                            null, null, null);
                }else if (handleResult==2){
                    jobstatus_t.setText("已完成");
                    jobstatus_t.setTextColor(Color.rgb(41, 171, 145));
                    jobstatus_t.setCompoundDrawablesWithIntrinsicBounds(ywc,
                            null, null, null);
                }
                holder.setItemViewOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent intent=new Intent();
                        if (handleResult==1){
                            intent.setClass(mContext,Activity_saomiao.class);
                            intent.putExtra("TAG","WEIXIU");
                            intent.putExtra("jobbean",dataBean);
                            startActivity(intent);
                            finish();
                        } else if (handleResult==2) {
                            intent.setClass(mContext,FaultCompleteActivity.class);
                            intent.putExtra("TAG","WEIXIU");
                            intent.putExtra("jobbean",dataBean);
                            startActivity(intent);
                        }



                    }
                });
            }
        };
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        xrvTask.setLayoutManager(linearLayoutManager);
        xrvTask.addItemDecoration(new SpaceItemDecoration(DisplayUtil.dip2px(
                mContext, 0.5f)));
        xrvTask.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                xrvTask.setLoadingMoreEnabled(true);
                dataBeanList.clear();
                pageNum=1;
                initData(pageNum ,jobResult);
            }

            @Override
            public void onLoadMore() {
                pageNum++;
                initData(pageNum ,jobResult);
            }
        });

        xrvTask.setAdapter(adapter);
    }

    private void initMyView() {

        setTitleText("故障处理");
        setleftTextVisibility(true);
        setleftImgVisibility(true);
        initData(pageNum, 1);
        dzx = getResources().getDrawable(
                R.mipmap.weixunjian);//待执行
        ywc = getResources().getDrawable(
                R.mipmap.yiwancheng);//已完成
        gq = getResources().getDrawable(
                R.mipmap.gq);//已过期
        sx = getResources().getDrawable(
                R.mipmap.sx);//失效
        wz = getResources().getDrawable(
                R.mipmap.wz);//未知

    }

    private void initData(int pagenum, int result) {
        Map map = new HashMap<String, Object>();
//        map.put("taskId",taskId);
//        map.put("jobResult",result+"");
        requestInspect = new RequestInspect();
        requestInspect.setPageNum(pagenum);
        requestInspect.setPageSize(pageSize);
        requestInspect.setParam(map);
        requestInspect.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        requestInspect.setAction(6);
        requestInspect.setTimeStamp(TimeUtils.getTime());
        requestInspect.setSign(MyApplication.getInstance().generateDvSign(requestInspect));
        faultJobLPresenter.GetFault(requestInspect);
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {
        wushuju.setVisibility(View.VISIBLE);
        ToastUtils.show(mContext,msg);
    }

    @Override
    public void FaultJob(FaultJobLBean faultJobLBean) {
        xrvTask.refreshComplete();
        xrvTask.loadMoreComplete();
        if (faultJobLBean.getResult() == 0) {
            List<FaultJobLBean.DataBean> data = faultJobLBean.getData();
            dataBeanList.addAll(data);

            if (faultJobLBean.getTotalpage() == pageNum) {
                xrvTask.setLoadingMoreEnabled(false);
            }
            if (pageNum == 1 && data.size() == 0) {
                wushuju.setVisibility(View.VISIBLE);
            } else {
                wushuju.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();

        }
    }
}
